<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" title="组选择器" scrollable no-close-on-backdrop>
        <div class="d-block text-center" id="tableDPRBody">
            <div class="form-group m-form__group row">
                <div class="col-md-6">
                    <el-select v-model="groupType" placeholder="请选择">
                        <el-option
                        v-for="item in [{value:0,label:'部门'},{value:1,label:'岗位'},{value:2,label:'角色'}]"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class="form-group m-form__group row">        
                <div class="col-md-6">      
                    <el-input placeholder="输入关键字进行过滤" style="width:200px" v-model="filterText"> </el-input>
                </div>  
            </div>
            <div class="form-group m-form__group row">
                <div class="col-md-6">
                    <el-tree 
                        :data="dataList" 
                        node-key="id"
                        class="filter-tree"
                        title="组" 
                        show-checkbox
                        :check-strictly="true"
                        :default-expand-all="false" 
                        :expand-on-click-node="false"
                        ref="lcOrgTree"
                        :filter-node-method="filterNode"
                        :props="defaultProps" >
                    </el-tree>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取 消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="doSelect">
                <i class="icon-xl fas fa-save"></i> 确 认
            </button>
        </template>
    </b-modal>
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating, dateformat } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            dataList: [],
            groupType:0,
            filterText:"",     
            defaultProps: {
                children: 'children',
                label: 'text'
            },
            sels: [], //当前选框选中的值
        }
    },
    components: {

    },
    watch: {
        "groupType":{
            handler:function(newVal,old){
                // if(newVal){
                    this.dataList = [];
                    this.initList();
                // }
            }
        },
        filterText(val) {
            this.$refs['lcOrgTree'].filter(val);  
        }
    },
    mounted() {

    },
    methods: {
        filterNode(value, data) {
            if (!value) return true;
            return data.text.indexOf(value) !== -1;
        },
        showModal(row) {
            this.filterText = "";
            this.$refs['my-modal'].show();
            this.initList();
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        initList() {
            showWating({ renderBody: "tableDPRBody" });
            var params = { };
            if(this.groupType == 0){
                apiUtil.get(process.env.VUE_APP_SYS_API + "/sysOrg/depart/tree", params).then(({ data }) => {
                    let result = eval("(" + data.data + ")");
                    this.dataList = result;
                });
            }
            if(this.groupType == 1){
                apiUtil.get(process.env.VUE_APP_SYS_API + "/sysOrg/post/tree", params).then(({ data }) => {
                    let result = eval("(" + data.data + ")");
                    this.dataList = result;
                });
            }
            if(this.groupType == 2){
                apiUtil.get(process.env.VUE_APP_OAUTH_API + "/oauthRole/trees", params).then(({ data }) => {
                    let result = eval("(" + data.data + ")");
                    this.dataList = result;
                });
            }
        },
        handleSelectionChange(sels) {//获取选中的值
            this.sels = sels;
        },
        search() {
            this.initList();          // 按新的pageNo和pageSize进行查询
        },        
        doSelect() {
            let data = this.$refs['lcOrgTree'].getCheckedNodes();
            if(data.length != 1){
                handleAlert("请选择一条记录", "warning", 3)
                return;
            }
            // 选择前提示
            this.$confirm("确认选择记录吗?", "提示", {type: "warning",}).then(() => {
                this.hideModal();//关闭窗体
                this.$emit("change", data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
            }).catch(()=>{});//注意这里，这里是重点！！！; 
        },
    }
}
</script>